package Presentacion.academico;

import clases.academico.Paralelo;
import global.Dialogos;
import global.Iconos;

public final class dlgCurso extends javax.swing.JDialog {
    
    private Iconos ico = new Iconos();
    
    public dlgCurso(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setTitle("REGISTRO DE CURSOS");
        setLocationRelativeTo(this);
        setResizable(false);
        iconos();
        iniciar();
        visiblidad(true);
    }
    private void iconos() {
        btnnuevo.setIcon(ico.getNuevo());
        btnguardar.setIcon(ico.getGuardar());
        btnsalir.setIcon(ico.getSalir());
        btnactualizar.setIcon(ico.getActualizar());
    }
    protected void iniciar() {
        frmConsultaCurso.objCurso.listarParalelo(cboParalelo);
        cboTipo.setSelectedIndex(0);
        frmConsultaCurso.objCurso.listarCurso(cboCurso);
        lblcategoria.setVisible(false);
        cboCategoria.setVisible(false);
        cboCurso.requestFocus();
    }
    
    public void visiblidad(boolean visibilidad) {
        if (visibilidad != true) {
            btnactualizar.setVisible(true);
            btnguardar.setVisible(false);
        } else {
            btnactualizar.setVisible(false);
            btnguardar.setVisible(true);
        }
    }
    
    private boolean ValidacionCampos() {
        if ("--Seleccione--".equals(this.cboCurso.getSelectedItem().toString())) {
            Dialogos.FaltaCampo("Curso");
            this.cboCurso.requestFocus();
            return false;
        } else if ("--Seleccione--".equals(cboParalelo.getSelectedItem().toString())) {
            Dialogos.FaltaCampo("Paralelo");
            cboParalelo.requestFocus();
            return false;
        } else if (this.cboTipo.getSelectedItem().toString().equals("--Seleccione--")) {
            Dialogos.FaltaCampo("Tipo");
            this.cboTipo.requestFocus();
            return false;
            
        } else if (this.cboTipo.getSelectedItem().toString().equals("NORMAL")) {
            if (this.cboCategoria.getSelectedItem().toString().equals("--Seleccione--")) {
                Dialogos.FaltaCampo("Categoria");
                this.cboCategoria.requestFocus();
                return false;
            }
        }
        return true;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cboParalelo = new javax.swing.JComboBox();
        cboTipo = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        btnParalelo = new javax.swing.JButton();
        lblcategoria = new javax.swing.JLabel();
        cboCategoria = new javax.swing.JComboBox();
        cboCurso = new javax.swing.JComboBox();
        btnCurso = new javax.swing.JButton();
        jToolBar4 = new javax.swing.JToolBar();
        btnnuevo = new javax.swing.JButton();
        btnguardar = new javax.swing.JButton();
        btnactualizar = new javax.swing.JButton();
        btnsalir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("CURSO");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel2.setText("PARALELO");

        cboTipo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "NORMAL", "ESPECIAL", "CLUB" }));
        cboTipo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboTipoActionPerformed(evt);
            }
        });

        jLabel4.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel4.setText("TIPO");

        btnParalelo.setText("+");
        btnParalelo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnParaleloActionPerformed(evt);
            }
        });

        lblcategoria.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        lblcategoria.setText("CATEGORIA");

        cboCategoria.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "BASICO", "BACHILLERATO" }));

        cboCurso.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "--Seleccione--", "OCTAVO AÑO BASICO", "NOVENO AÑO BASICO", "DECIMO AÑO BASICO", "PRIMER AÑO DE BACHILLERATO", "SEGUNDO AÑO DE BACHILLERATO", "TERCER AÑO DE BACHILLERATO" }));

        btnCurso.setText("+");
        btnCurso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCursoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel1)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4)
                    .addComponent(lblcategoria))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cboCategoria, javax.swing.GroupLayout.Alignment.LEADING, 0, 282, Short.MAX_VALUE)
                    .addComponent(cboParalelo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboTipo, javax.swing.GroupLayout.Alignment.LEADING, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(cboCurso, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(7, 7, 7)
                        .addComponent(btnParalelo))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCurso)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
                .addGap(24, 24, 24)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cboCurso, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCurso))
                .addGap(22, 22, 22)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(cboParalelo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnParalelo))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cboTipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addGap(25, 25, 25)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(cboCategoria, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lblcategoria))
                .addGap(25, 25, 25))
        );

        jToolBar4.setBorder(null);
        jToolBar4.setFloatable(false);
        jToolBar4.setBorderPainted(false);

        btnnuevo.setText("Nuevo");
        btnnuevo.setFocusable(false);
        btnnuevo.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnnuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnnuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnnuevoActionPerformed(evt);
            }
        });
        jToolBar4.add(btnnuevo);

        btnguardar.setText("Guardar");
        btnguardar.setFocusable(false);
        btnguardar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnguardar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnguardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnguardarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnguardar);

        btnactualizar.setText("Actualizar");
        btnactualizar.setFocusable(false);
        btnactualizar.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnactualizar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnactualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnactualizarActionPerformed(evt);
            }
        });
        jToolBar4.add(btnactualizar);

        btnsalir.setText("Salir");
        btnsalir.setFocusable(false);
        btnsalir.setHorizontalTextPosition(javax.swing.SwingConstants.RIGHT);
        btnsalir.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnsalir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsalirActionPerformed(evt);
            }
        });
        jToolBar4.add(btnsalir);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jToolBar4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar4, javax.swing.GroupLayout.PREFERRED_SIZE, 54, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnnuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnnuevoActionPerformed
        iniciar();
        visiblidad(true);

    }//GEN-LAST:event_btnnuevoActionPerformed

    private void btnguardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnguardarActionPerformed
        if (this.ValidacionCampos() == true) {
            if (frmConsultaCurso.objCurso.ExistenciaCurso(this.cboCurso.getSelectedItem().toString() + " "
                    + cboParalelo.getSelectedItem().toString()) == true) {
                global.Mensajes.mostrarMensaje("Advertencia!", "El curso ya existe", 2);
                
            } else {
                frmConsultaCurso.objCurso.setCurso(cboCurso.getSelectedItem().toString());
                frmConsultaCurso.objCurso.setTipo(cboTipo.getSelectedItem().toString());
                if (cboTipo.getSelectedItem().equals("NORMAL")) {
                    frmConsultaCurso.objCurso.setCategoria(cboCategoria.getSelectedItem().toString());
                }
                frmConsultaCurso.objCurso.setParalelo(cboParalelo.getSelectedItem().toString());
                frmConsultaCurso.objCurso.sacarIdParalelo(cboParalelo.getSelectedItem().toString());
                if (frmConsultaCurso.objCurso.guardarCurso() == true) {
                    frmConsultaCurso.generartabla();
                    iniciar();
                }
                
            }
        }
    }//GEN-LAST:event_btnguardarActionPerformed

    private void btnactualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnactualizarActionPerformed
        if (this.ValidacionCampos() == true) {
            if (frmConsultaCurso.objCurso.ExistenciaCurso(this.cboCurso.getSelectedItem().toString() + " "
                    + cboParalelo.getSelectedItem().toString()) == true) {
                global.Mensajes.mostrarMensaje("Advertencia!", "El curso ya existe", 2);
                
            } else {
                frmConsultaCurso.objCurso.setCurso(cboCurso.getSelectedItem().toString());
                frmConsultaCurso.objCurso.setTipo(cboTipo.getSelectedItem().toString());
                if (cboTipo.getSelectedItem().equals("NORMAL")) {
                    frmConsultaCurso.objCurso.setCategoria(cboCategoria.getSelectedItem().toString());
                }
                frmConsultaCurso.objCurso.setParalelo(cboParalelo.getSelectedItem().toString());
                frmConsultaCurso.objCurso.sacarIdParalelo(cboParalelo.getSelectedItem().toString());
                if (frmConsultaCurso.objCurso.actualizarCurso() == true) {
                    frmConsultaCurso.generartabla();
                    dispose();
                }
            }
        }
    }//GEN-LAST:event_btnactualizarActionPerformed

    private void btnsalirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsalirActionPerformed
        dispose();
        iniciar();
    }//GEN-LAST:event_btnsalirActionPerformed

    private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
        iniciar();
    }//GEN-LAST:event_formWindowClosed

    private void btnParaleloActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnParaleloActionPerformed
        String paralelo = "";
        paralelo = global.Dialogos.mostrarDialogoconTexto("AGREGAR PARALELO", null);
        if (paralelo != null && !"".equals(paralelo)) {
            Paralelo objParalelo = new Paralelo();
            objParalelo.setParalelo(paralelo.toUpperCase());
            if (objParalelo.guardarParalelo() == true) {
                cboParalelo.addItem(paralelo.toUpperCase());                
            }             
        }

//dlgParalelo paralelo = new dlgParalelo(null, true);
        //paralelo.setVisible(true);
    }//GEN-LAST:event_btnParaleloActionPerformed

    private void cboTipoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboTipoActionPerformed
        if (cboTipo.getSelectedItem().equals("NORMAL")) {
            lblcategoria.setVisible(true);
            cboCategoria.setVisible(true);
        } else {
            lblcategoria.setVisible(false);
            cboCategoria.setVisible(false);
        }
    }//GEN-LAST:event_cboTipoActionPerformed

    private void btnCursoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCursoActionPerformed
        String curso = "";
        curso = global.Dialogos.mostrarDialogoconTexto("AGREGAR CURSO", null);
        if (curso != null && !"".equals(curso)) {
            this.cboCurso.addItem(curso.toUpperCase());
        }
    }//GEN-LAST:event_btnCursoActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(dlgCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(dlgCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(dlgCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(dlgCurso.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                dlgCurso dialog = new dlgCurso(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCurso;
    private javax.swing.JButton btnParalelo;
    private javax.swing.JButton btnactualizar;
    private javax.swing.JButton btnguardar;
    private javax.swing.JButton btnnuevo;
    private javax.swing.JButton btnsalir;
    public javax.swing.JComboBox cboCategoria;
    public javax.swing.JComboBox cboCurso;
    public javax.swing.JComboBox cboParalelo;
    public javax.swing.JComboBox cboTipo;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JToolBar jToolBar4;
    private javax.swing.JLabel lblcategoria;
    // End of variables declaration//GEN-END:variables
}
